using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace FallenValkyrie {
    class EnemyGenerator
    {
        /*
        protected int minNumUnits;
        protected int maxNumUnits;

        EnemyType1[] enemies;
        Queue<EnemyType1> freeEnemies;

        public void Initialize(){
            enemies = new EnemyType1[maxNumUnits];
            freeEnemies = new Queue<EnemyType1>(maxNumUnits);
            for (int i = 0; i < enemies.Length; i++)
            {
                enemies[i] = new EnemyType1();
                freeEnemies.Enqueue(enemies[i]);
            }
        }
        
        public void AddEnemy(Vector2 where) {
            int numEnemies = 
        }
        */

        //  Variables
        private static int MAX = 50;
        private static Random random = new Random();
        public EnemyType1[] enemy;
        public Vector2[] feetPosition;
        public int enemyNum;
        public string[] sightDirection;
        public SpriteEffects[] spriteEffects;
        public int[] HP;
        public Rectangle[] enemyRect;
        public Texture2D[] enemyTex;
        public bool atkAnimation;
        AI ai;
        public bool[] GenGetHitState;
        public bool[] doneGetHit;
        public bool[] dead;
        //  Constructor
        public EnemyGenerator(int enemy_number, float enemySpeed, float enemyAtkRange) {
            enemyNum       = enemy_number;
            feetPosition   = new Vector2[enemyNum];
            sightDirection = new string[enemyNum];
            spriteEffects  = new SpriteEffects[enemyNum];
            HP             = new int[enemyNum];
            enemyRect      = new Rectangle[enemyNum];
            enemyTex       = new Texture2D[enemyNum];
            ai             = new AI(enemySpeed, enemyAtkRange);
            GenGetHitState = new bool[enemyNum];
            doneGetHit     = new bool[enemyNum];
            dead = new bool[enemyNum];
        }

        bool generated = false;

        //  Methods
        public void addEnemy(ContentManager content)  {
            if (!generated) {
                //if (enemyNum < MAX) {
                    enemy = new EnemyType1[enemyNum];
                    
                    for (int i = 0; i < enemyNum; i++) {
                        enemy[i] = new EnemyType1();
                        enemyTex[i] = content.Load<Texture2D>("GUI/hpBar");
                        enemy[i].Load(content);
                        enemy[i].HP = 200;
                        enemy[i].feetPosition.X = i * 800;
                        HP[i] = enemy[i].HP;
                        dead[i] = false;
                        generated = true;
                    }
               // }
            }
        }

        public void updateEnemy(Vector2 offset, GameTime gt, int i) {
            ai.UpdateAI(offset, enemy[i].feetPosition, ref enemy[i].mSpeed, 
                ref enemy[i].mDirection, ref enemy[i].State_AI, ref enemy[i].StateEnd, gt);
            Console.WriteLine(enemyNum);
            atkAnimation = enemy[i].DmgAnimation;
            enemy[i].Update(gt);  
        }

        public void drawEnemy(SpriteBatch sb, int i) {
            sb.Begin();
                enemyRect[i] = new Rectangle((int)enemy[i].feetPosition.X, (int)enemy[i].feetPosition.Y, 108, 144);
                enemy[i].Draw(sb);
            sb.End();
        }

        public static float RandomBetween(float min, float max) {
            return min + (float)random.NextDouble() * (max - min);
        } 

        public static Random Random {
            get { return random; }
        }

    }
}
